package ext.tianma.change.mvc.builder;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import wt.access.AccessControlHelper;
import wt.access.AccessPermission;
import wt.change2.WTChangeOrder2;
import wt.fc.WTObject;
import wt.lifecycle.State;
import wt.org.WTPrincipal;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.util.WTException;

import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.TableConfig;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.beans.NmHelperBean;

import ext.com.iba.IBAUtil;
import ext.tianma.change.util.ChangeGetModifiedFinishedGood;

/**
 * “变更通告”页面添加的一个“修改过的成品”表格
 * @author KWang
 *
 */
@ComponentBuilder(value = "ecn.modifiedFinishedGood.table.builder")
public class ModifiedFinishedGoodTableBuilder extends AbstractComponentBuilder{
	//已解决
	private static final String RESOLVED = "RESOLVED";
	
	public ModifiedFinishedGoodTableBuilder(){
		
	}

	@Override
	public ComponentConfig buildComponentConfig(ComponentParams componentParams )
			throws WTException {
		ComponentConfigFactory componentconfigfactory = getComponentConfigFactory();
		
		TableConfig tableconfig = componentconfigfactory.newTableConfig();
		tableconfig.setId("ecn.modifiedFinishedGood.table.builder") ;
		
		tableconfig.setLabel("从DCR关联的成品中选择DCN实施的成品") ;
		tableconfig.setSelectable(true);
		
		//tableconfig.setActionModel("changeTask.affectedItems.table.create_edit");
		isshowActionModel( componentParams , tableconfig ) ;
		
		
		ColumnConfig  iconConfig = componentconfigfactory.newColumnConfig("type_icon", true);
		iconConfig.setLabel("");
//		numberConfig.setDataUtilityId("MassChangeNumberDataUtility");
		tableconfig.addComponent(iconConfig);
		
		ColumnConfig  numberConfig = componentconfigfactory.newColumnConfig("number", true);
		numberConfig.setLabel("编号");
//		numberConfig.setDataUtilityId("MassChangeNumberDataUtility");
		tableconfig.addComponent(numberConfig);
		
		ColumnConfig  nameConfig = componentconfigfactory.newColumnConfig("name", true);
		nameConfig.setLabel("名称");
//		numberConfig.setDataUtilityId("MassChangeNumberDataUtility");
		tableconfig.addComponent(nameConfig);
		
		ColumnConfig  viewConfig = componentconfigfactory.newColumnConfig("view", true);
		viewConfig.setLabel("视图");
//		numberConfig.setDataUtilityId("MassChangeNumberDataUtility");
		tableconfig.addComponent(viewConfig);
		
		ColumnConfig  versionConfig = componentconfigfactory.newColumnConfig("version", true);
		versionConfig.setLabel("版本");
//		numberConfig.setDataUtilityId("MassChangeNumberDataUtility");
		tableconfig.addComponent(versionConfig);
		
		ColumnConfig  stateConfig = componentconfigfactory.newColumnConfig("state", true);
		stateConfig.setLabel("状态");
//		numberConfig.setDataUtilityId("MassChangeNumberDataUtility");
		tableconfig.addComponent(stateConfig);
		
		return tableconfig ;
	}
	
	/**
	 * 控制
	 * @param componentParams
	 * @param tableconfig
	 */
	private void isshowActionModel(ComponentParams componentParams, TableConfig tableconfig) {
		boolean notIsResolved = true;
		boolean hasModifiedPrivilege = true ;
		
		
		try {
			WTObject wtObject =  getPageObject( componentParams );
			
			if( wtObject != null && wtObject instanceof WTChangeOrder2 ){
				WTChangeOrder2 ecn = ( WTChangeOrder2 )wtObject;
				notIsResolved = isResolvedState( ecn ) ;
				hasModifiedPrivilege = hasModifiedPrivilege( ecn ) ;			
			}
			
		} catch (WTException e) {			
			e.printStackTrace();
			
		}finally{
			System.out.println( "Debug    notIsResolved:"+notIsResolved +"   hasModifiedPrivilege:"+hasModifiedPrivilege );
			if( notIsResolved || hasModifiedPrivilege ){
				tableconfig.setActionModel("ModifiedFinishedGood");
			}		
		}
	}
	
	/**
	 * 判断用户是否有编辑权限
	 * @param ecn
	 * @return
	 * @throws WTException 
	 */
	private boolean hasModifiedPrivilege( WTChangeOrder2 ecn ) throws WTException {
		boolean hasModifiedPrivilege = false;
		WTPrincipal principal =  SessionHelper.manager.getPrincipal();
		
		if( principal != null ){
			//判断当前角色是否都当前对象有修改权限
			hasModifiedPrivilege = AccessControlHelper.manager.hasAccess(principal, ecn, AccessPermission.MODIFY);		
		}
		return hasModifiedPrivilege;
	}
	
	/**
	 * 判断ECN状态是否为“已解决”状态
	 * @param ecn
	 * @return
	 */
	private boolean isResolvedState(WTChangeOrder2 ecn) {
		boolean notIsResolved = false;
		if( ecn != null ){
			State partState = ecn.getLifeCycleState();
			
			if( partState != null ){
				String state = partState.toString();
				if( !state.equals( RESOLVED ) ){
					notIsResolved = true;
				}
			}		
		}
		return notIsResolved;
	}
	
	

	@Override
	public Object buildComponentData(ComponentConfig componentConfig, ComponentParams componentParams)
			throws Exception {
		
		List<WTPart> list = new ArrayList<WTPart>();
		
		WTObject wtobject = getPageObject(componentParams);
		
		if( ( wtobject != null ) && ( wtobject instanceof WTChangeOrder2 ) ){
			
			WTChangeOrder2 ecn = ( WTChangeOrder2 )wtobject;
			System.out.println( "Debug    buildComponentData获取到的ecn:"+ecn );
			list = getChangeModifiedFinishedGood( ecn );	
			System.out.println( "Debug  获取到的对象list:"+list.size() );
			
		}
		
		return list;
	}

	private  WTObject getPageObject(ComponentParams componentParams)
			throws WTException {
		NmHelperBean nmhelperbean = ((JcaComponentParams) componentParams ).getHelperBean();
		NmCommandBean nmcommandbean = nmhelperbean.getNmCommandBean();
		NmOid nmoid =  nmcommandbean.getActionOid();
		
		WTObject wtobject = (WTObject) nmoid.getWtRef().getObject();
		return wtobject;
	}
	
	private static List<WTPart> getChangeModifiedFinishedGood( WTChangeOrder2  ecn ){
		List<WTPart> list =new ArrayList<WTPart>();
		
		try {
			
			String ibaModifiedFinishedGood = (String) IBAUtil.getIBAValue(ecn, "modifiedFinishedGood");
			String number = "";
			String view = "";
			String version = "";
			
			System.out.println( "Debug     ibaModifiedFinishedGood:"+ibaModifiedFinishedGood );
			
			if( ibaModifiedFinishedGood != null ){
				
				String[] finishedGood = ibaModifiedFinishedGood.split( ";" );
				
				System.out.println( "Debug     finishedGood:"+finishedGood.length );
				
				if( finishedGood != null ){
					for (int i = 0; i < finishedGood.length; i++) {					
						
						if( ( finishedGood[i] != null ) &&  ( !finishedGood[i].equals( "" ) ) ){
							
							String[] fied = finishedGood[i].split( "," );
							
							System.out.println( "Debug     fied:"+fied.length );
							
							if( ( fied != null ) && ( fied.length == 3 ) ){
								
								number = fied[0];
								view = fied[1];
								version = fied[2];
								
								System.out.println( "Debug   number:"+number +"  view:"+view +"  version:"+version );
								
								WTPart part = getWtPartByNumviewAndVersion(number, view, version);
								
								if( part != null ){
									System.out.println( "Debug   part:"+part );
									list.add(part);
								}
							}
						}	
					}
				}		
			}
			
		} catch (RemoteException e) {
			
			e.printStackTrace();
		} catch (WTException e) {

			e.printStackTrace();
		}
		
		return list;
	}
	
	private static WTPart getWtPartByNumviewAndVersion( String number , String view , String version ){
		WTPart part = null;
		
		if( number != null && view != null && version != null ){
			
			try {
				System.out.println( "Debug     开始运行获取getWtPartByNumviewAndVersion代码..." );
				part = ChangeGetModifiedFinishedGood.getWTPartByNumViewAndVersion( number , view , version );
				
			} catch (WTException e) {
				
				e.printStackTrace();
			}
			
		}
		
		return part;
	}
	
	public static void main(String[] args) {
		
	}

}
